package zhoufu.sz.sanxiao_check.presents;

import android.text.TextUtils;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.models.BaseModel;
import zhoufu.sz.sanxiao_check.models.FireNoticeModel;
import zhoufu.sz.sanxiao_check.net.BasicRequestRepository;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.ui.contract.BasicMsgItem2Contract;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/26
 */
public class BasicItem2Presenter implements BasicMsgItem2Contract.Presenter
{

    private BasicRequestRepository mRepository;

    private BasicMsgItem2Contract.View view;

    private String signPath;

    @Inject
    public BasicItem2Presenter(BasicRequestRepository mRepository)
    {
        this.mRepository = mRepository;
    }

    public void bindView(BasicMsgItem2Contract.View view)
    {
        this.view = view;
    }


    @Override public void onStart()
    {
        mRepository.GetAgreement(view.SID(), new RequestHelper.RequestCallBack<FireNoticeModel>()
        {
            @Override public void onPullStart()
            {
                view.pullStart();
            }

            @Override public void onPullFailud(Throwable throwable)
            {
                view.pullFaild();
            }

            @Override public void onPullEnd()
            {
                view.pullEnd();
            }

            @Override public void onPullSuccessed(FireNoticeModel s)
            {
                if ( s.isSuccess() )
                {
                    if ( s.getData().size() > 0 )
                    {
                        view.setModel(s.getData().get(0));
                    }
                } else
                {
                    view.showToast(s.getErrMsg());
                }
            }
        });
    }

    @Override public void onPause()
    {

    }

    @Override public void onDestroy()
    {
        mRepository.onDestory();
    }

    @Override public void update()
    {
        if ( TextUtils.isEmpty(signPath) )
        {
            view.showToast("请场所负责人签名");
            return;
        }
        mRepository.UpdateAgreement(view.SID(), signPath,
                new RequestHelper.RequestCallBack<BaseModel>()
                {
                    @Override public void onPullStart()
                    {
                        view.pullStart();
                    }

                    @Override public void onPullFailud(Throwable throwable)
                    {
                        view.pullFaild();
                    }

                    @Override public void onPullEnd()
                    {
                        view.pullEnd();
                    }

                    @Override public void onPullSuccessed(BaseModel s)
                    {
                        view.pullEnd();
                        if ( s.isSuccess() )
                        {
                            view.updateSuccese();
                        } else
                        {
                            view.showToast(s.getErrMsg());
                        }
                    }
                });
    }

    public void setSignPath(String signPath)
    {
        this.signPath = signPath;
    }
}
